﻿@using CTTool.Web.Controllers;

@{
    ViewBag.Title = "将JSON Data 转换为 Object C Model";
}

@section styles
{
    <style type="text/css">
        label {
            /*display: inline-block;
            width:150px;*/
        }

        dt {
            font-weight: bold;
            -webkit-margin-start: 10px;
        }

        dd {
            -webkit-margin-start: 20px;
        }

        th {
            text-align: center;
            border: solid 1px;
        }

        .pb10 {
            padding-bottom: 10px;
        }
    </style>
}

<div class="float-left" style="width:45%;">
    <div class="pb10">
        <table cellpadding="0" border="1" style="width:100%;">
            <thead>
                <tr>
                    <th colspan="4">基本配置信息</th>
                </tr>
                <tr>
                    <th>Company</th>
                    <th align="left" colspan="3"><input id="codeCompany" placeholder="ToolWebCompany" value="@(JsonToObjectCController.Company)" maxlength="50" /></th>
                </tr>
                <tr>
                    <th>Project</th>
                    <th align="left" colspan="3"><input id="codeProject" placeholder="ToolWebProject" value="@(JsonToObjectCController.Project)" maxlength="50" /></th>
                </tr>
                <tr>
                    <th>Author</th>
                    <th align="left" colspan="3"><input id="codeAuthor" placeholder="ToolWebAuthor" value="@(JsonToObjectCController.Author)" maxlength="50" /></th>
                </tr>
            </thead>
        </table>
    </div>
    <div>
        <label>JSON 字符串</label>
        <textarea id="jsonData" style="width:100%; height:100px">@("{\"Code\":0,\"Data\":{\"Cellphone\":\"15882116817\",\"EstateID\":2305,\"ActivityID\":50,\"CustomerName\":\"Ty\",\"UserID\":559,\"UseDKBService\":0},\"Message\":\"\"}")</textarea>
    </div>
    <div style="display:none;">
        <input id="analyseBtn" type="button" value="分析" />
        <div id="analyseParam"></div>
    </div>
    @*<div>
            <label>c:</label>
            <input id="codeCompany" placeholder="ToolWebCompany" value="ToolWebCompany" maxlength="50" />
        </div>
        <div>
            <label>Code Project:</label>
            <input id="codeProject" placeholder="ToolWebProject" value="ToolWebProject" maxlength="50" />
        </div>
        <div>
            <label>Code Author:</label>
            <input id="codeAuthor" placeholder="ToolWebAuthor" value="ToolWebAuthor" maxlength="50" />
        </div>*@
    <div>
        <div>
            <input id="tanslateBtn" type="button" value="预览" style="display:none;" />
            <input id="dowloadBtn" type="button" value="下载" disabled="disabled" />
        </div>
        <div style="color:red; font-weight:bold;" id="message"></div>
    </div>
    <div id="fileTree" class="ztree"></div>
</div>
<div class="float-right" style="width:45%;">
    <label>Object-C Model</label>
    <div id="ObjectCCode"></div>
</div>

@section scripts
{
<link rel="stylesheet" href="/scripts/ztree-v3/css/zTreeStyle/zTreeStyle.css" type="text/css" />
<script type="text/javascript" src="/scripts/ztree-v3/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="/scripts/jquery.json.min.js"></script>
<script type="text/javascript" src="/scripts/js.cookie.js"></script>
<script type="text/javascript">
    $(function () {
        previewFile = function (treeNode) {
            treeNode.dataJson = $("#jsonData").val();
            $.post("/JsonToObjectC/PreviewFile", treeNode, function (data) {
                if (data.Success) {
                    $("#ObjectCCode").html(data.Data);
                    $("#message").html("");
                    var $objectCCode = $("#ObjectCCode");
                    $objectCCode.find("h5").click(function () {
                        $(this).next("dl").toggle();
                    });
                    $objectCCode.find("dt").click(function () {
                        $(this).next("dd").toggle();
                    });
                } else {
                    $("#ObjectCCode").html("");
                    $("#message").html(data.Message);
                }
            });
        }
        beforeRename = function (treeId, treeNode, newName, isCancel) {
            return true;
        }
        onRename = function (event, treeId, treeNode, isCancel) {
            var treeObj = $.fn.zTree.getZTreeObj(treeId);
            $.each(treeNode.children, function (i, item) {
                if (item.originName === treeNode.originName
                    && item.name != treeNode.name) {
                    item.name = treeNode.name + item.extention;
                    treeObj.updateNode(item);
                }
            });
        }
        var requestParam = function () {
            return {
                json: $("#jsonData").val()
            };
        };
        $("#analyseBtn").click(function () {
            $.get("/JsonToObjectC/Analyse", function (data) {
                $("#analyseParam").html(data);
            });
        });
        $("#jsonData").change(function () {
            $("#dowloadBtn").attr({ "disabled": "disabled" });
            $.fn.zTree.destroy();
            var treeSetting = {
                async: {
                    enable: true,
                    url: '/JsonToObjectC/FileTree',
                    otherParam: {
                        jsonData: $("#jsonData").val()
                    },
                },
                check: {
                    enable: true,
                },
                edit: {
                    enable: true,
                    showRemoveBtn: false,
                    showRenameBtn: function (treeId, treeNode) {
                        return !treeNode.nocheck;
                    },
                },
                callback: {
                    onClick: function (event, treeId, treeNode) {
                        previewFile(treeNode);
                    },
                    beforeRename: beforeRename,
                    onRename: onRename,
                    onAsyncSuccess: function (event, treeId, treeNode, msg) {
                        $("#dowloadBtn").removeAttr("disabled");
                        return msg.Data;
                    },
                    onAsyncError: function (event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.statusText);
                    },
                },
            };
            $.fn.zTree.init($("#fileTree"), treeSetting);
        });
        $("#tanslateBtn").click(function () {
            $("#ObjectCCode").html("");
            $("#message").html("");
            $.post("/JsonToObjectC/Translate", requestParam(), function (data) {
                if (data.Success) {
                    $("#ObjectCCode").html(data.Data);
                    $("#message").html("");
                    var $objectCCode = $("#ObjectCCode");
                    $objectCCode.find("h5").click(function () {
                        $(this).next("dl").toggle();
                    });
                    $objectCCode.find("dt").click(function () {
                        $(this).next("dd").toggle();
                    });
                } else {
                    $("#ObjectCCode").html("");
                    $("#message").html(data.Message);
                }
            });
        });
        $("#dowloadBtn").click(function () {
            $("#message").html("");
            var treeObj = $.fn.zTree.getZTreeObj("fileTree");
            var paramData = {
                nodes: treeObj.getNodes(),
                json: $("#jsonData").val(),
            };
            $.post("/JsonToObjectC/Download", { jsonData: $.toJSON(paramData) }, function (data) {
                if (data.Success) {
                    window.open(data.Data);
                } else {
                    $("#ObjectCCode").html("");
                    $("#message").html(data.Message);
                }
            });
        });
        $("#jsonData").change();
        updateBasicConfig = function () {
            Cookies.set("JsonToObjectC_Company", $.trim($("#codeCompany").val()));
            Cookies.set("JsonToObjectC_Project", $.trim($("#codeProject").val()));
            Cookies.set("JsonToObjectC_Author", $.trim($("#codeAuthor").val()));
        };
        $("#codeCompany").bind("change", updateBasicConfig);
        $("#codeProject").bind("change", updateBasicConfig);
        $("#codeAuthor").bind("change", updateBasicConfig);
    });
</script>
}